/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetluc;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Lone_Wolf
 */
public class ConsulterFacture extends JPanel {

    private Fenetre fenetre;
    Font f = new Font("Times New Roman", Font.BOLD, 24);
    Font f2 = new Font("Times New Roman", Font.BOLD, 18);
    private Facture facture;
    private GridBagLayout gbl;
    private GridBagConstraints gbc;
    private JLabel lblNumFacture;
    private JLabel lblMontantTotal;
    private JLabel lblClient;
    private JLabel lblPage;
    private JButton btnPageSuiv;
    private JButton btnPagePrec;
    private JButton btnAnnuler;
    private JButton btnModifier;
    private JButton btnPrint;
    private int pageCourante;
    private int nbeEntreesTotal;
    private int nbeEntreesParPage = 10;

    public ConsulterFacture(Fenetre fenetre, SearchResult result) {
        this.fenetre = fenetre;
        this.facture = result.getFacture();

        //fenetre.setSize(510, 540);
        fenetre.setTitle("Consulter " + facture.getNumero());
        fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        pageCourante = 1;
        nbeEntreesTotal = facture.getNbeEntrees();

        initComposants();
        construireInterface();

        fenetre.setVisible(true);
    }

    private void initComposants() {
        lblNumFacture = new JLabel("# " + facture.getNumero());
        lblMontantTotal = new JLabel("Total: " + facture.getCoutTotal() + "$,"
                + " Chargé : " + facture.getMontantCharge() + "$");
        lblClient = new JLabel("[" + facture.getClient().getNom() + "]");
        lblNumFacture.setFont(f);
        lblClient.setFont(f);
        lblMontantTotal.setFont(f2);
        lblPage = new JLabel(calculPage());

        btnAnnuler = new JButton("Retour");
        btnModifier = new JButton("Modifier");
        btnPageSuiv = new JButton("Page Suivante");
        btnPagePrec = new JButton("Page Précédente");
        btnPrint = new JButton("Imprimer");

        btnPagePrec.addActionListener(new GererEvenements());
        btnPageSuiv.addActionListener(new GererEvenements());
        btnAnnuler.addActionListener(new GererEvenements());
        btnModifier.addActionListener(new GererEvenements());
        btnPrint.addActionListener(new GererEvenements());
    }
    
    private String calculPage(){
        String temp = "Page " + pageCourante + "/";
        if (nbeEntreesTotal == 0){
            temp = temp + "1";
        } else if (nbeEntreesTotal%nbeEntreesParPage == 0){
            temp = temp + ((nbeEntreesTotal / nbeEntreesParPage));
        } else {
            temp = temp + ((nbeEntreesTotal / nbeEntreesParPage) + 1);
        }
        
        return temp;
    }

    private void construireInterface() {
        gbl = new GridBagLayout();
        gbc = new GridBagConstraints();
        this.setLayout(gbl);
        gbc.fill = GridBagConstraints.BOTH;

        makeSeparator();
        makeBorder();
        gbc.weightx = 0.0;                          //Numero et client
        gbl.setConstraints(lblNumFacture, gbc);
        this.add(lblNumFacture);
        makeBorder();
        gbc.weightx = 0.0;
        gbl.setConstraints(lblClient, gbc);
        this.add(lblClient);
        makeBorder();
        gbc.weightx = 0.1;
        gbc.fill = gbc.NONE;
        gbc.anchor = gbc.LINE_END;
        gbl.setConstraints(btnModifier, gbc);
        this.add(btnModifier);
        gbc.gridwidth = gbc.REMAINDER;
        makeBorder();
        makeSeparator();
        makeSeparator();

        buildResults();                         //Résultats

        makeSeparator();
        makeBorder();
        gbc.weightx = 0.1;                      //Page suiv/prec
        gbc.fill = gbc.NONE;
        gbc.anchor = gbc.LINE_END;
        gbl.setConstraints(btnPagePrec, gbc);
        this.add(btnPagePrec);
        makeBorder();
        gbc.weightx = 0.1;
        gbc.anchor = gbc.CENTER;
        gbl.setConstraints(lblPage, gbc);
        this.add(lblPage);
        gbc.weightx = 0.1;
        gbc.fill = gbc.NONE;
        gbc.anchor = gbc.LINE_START;
        gbc.gridwidth = gbc.RELATIVE;
        gbl.setConstraints(btnPageSuiv, gbc);
        this.add(btnPageSuiv);
        makeBorder();

        makeSeparator();                        //Boutons
        makeSeparator();
        makeSeparator();
        makeBorder();
        gbc.weightx = 0.1;
        gbc.fill = gbc.NONE;
        gbc.anchor = gbc.LINE_START;
        gbl.setConstraints(btnAnnuler, gbc);
        this.add(btnAnnuler);
        //makeBorder();
        //makeBorder();
        gbc.gridwidth = gbc.RELATIVE;
        gbc.anchor = gbc.LINE_END;
        gbl.setConstraints(lblMontantTotal, gbc);
        this.add(lblMontantTotal);
        makeBorder();
        makeSeparator();
        makeSeparator();
        makeBorder();
        gbc.gridwidth = gbc.REMAINDER;
        gbc.anchor= gbc.LINE_START;
        gbl.setConstraints(btnPrint, gbc);
        this.add(btnPrint);
        makeSeparator();
    }

    private void buildResults() {
        int debut = (pageCourante - 1) * nbeEntreesParPage;
        int fin = debut + nbeEntreesParPage;

        for (int i = debut; i < facture.getNbeEntrees() && debut < fin; i++) {
            makeBorder();
            JButton btnTemp = buildEntree(facture.getEntree(i));
            gbc.weightx = 0.1;
            gbc.fill = gbc.HORIZONTAL;
            gbc.gridwidth = gbc.RELATIVE;
            gbl.setConstraints(btnTemp, gbc);
            this.add(btnTemp);
            gbc.gridwidth = gbc.REMAINDER;
            makeBorder();

            debut++;
        }
    }

    private JButton buildEntree(Entree entree) {
        JButton temp = new JButton();
        temp.setHorizontalAlignment(JButton.LEADING);
        temp.setText("<html><b><i><font size=+0>" + entree.getEmploye().getNom() + "</b></i></font>"
                    + "     ---     " + entree.getTache() + " --- " + entree.getTemps()
                    + " heures pour " + entree.getCout() + "$ --- le "
                    + entree.getDay() + "-"
                    + entree.getMonth() + "-"
                    + entree.getYear() + "</html>");
        temp.setPreferredSize(new Dimension(457, 25));

        return temp;
    }

    private void makeSeparator() {
        JLabel sep = new JLabel(" ");
        gbc.gridwidth = gbc.REMAINDER;
        gbl.setConstraints(sep, gbc);
        this.add(sep);
        gbc.gridwidth = gbc.CENTER;
        gbc.weighty = 0;
    }

    private void makeBorder() {
        gbc.weightx = 0.0;
        JLabel sep = new JLabel("   ");
        gbl.setConstraints(sep, gbc);
        this.add(sep);
        gbc.gridwidth = gbc.CENTER;
    }

    private void updatePage() {
        lblPage.setText("Page " + pageCourante + "/" + ((nbeEntreesTotal / nbeEntreesParPage) + 1));
        this.removeAll();
        this.invalidate();
        this.repaint();
        initComposants();
        construireInterface();
        this.invalidate();
        this.repaint();
    }

    class GererEvenements implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == btnAnnuler) {
                try {
                    fenetre.AfficherEcranRecherche();
                } catch (IOException ex) {
                } catch (ClassNotFoundException ex) {
                }
            } else if (e.getSource() == btnModifier) {
                fenetre.afficherModifierFacture(facture);
            } else if (e.getSource() == btnPageSuiv) {
                if (pageCourante * nbeEntreesParPage < nbeEntreesTotal) {
                    pageCourante++;
                }
                updatePage();
            } else if (e.getSource() == btnPagePrec) {
                if (pageCourante > 1) {
                    pageCourante--;
                }
                updatePage();
            } else if (e.getSource() == btnPrint){
                new PrintFacture().PrintFacture(facture);
            }
        }
    }
}
